<template>
  <div class="comment" :class="{ load: complete }">
    <Loading v-if="complete"></Loading>
    <template v-else>
      <div class="back">
        <img src="../assets/drl.png" @click="toback" />
        <span>评论({{ total }})</span>
      </div>
      <div class="song">
        <div class="songimg">
          <img :src="thisSong.al.picUrl | imgURL(90)" alt="" />
        </div>
        <div class="songtitle">
          <h1>{{ thisSong.name }}</h1>
          <span>
            -
            <span class="name" v-for="singer in thisSong.ar" :key="singer.id">
              {{ singer.name }}
            </span>
          </span>
          <span>{{ thisSong.al.name }}</span>
        </div>
      </div>
      <div class="com">
        <h1>评论区</h1>
        <ul>
          <ComItem
            v-for="item in comments"
            :key="item.commentId + Math.random()"
            :item="item"
          ></ComItem>
        </ul>
        <Loading v-if="tocontinue"></Loading>
        <h3 @click="load" v-else>加载更多</h3>
      </div>
    </template>
  </div>
</template>

<script>
import ComItem from "@/components/ComItem.vue";
import Loading from "@/components/Loading.vue";
export default {
  data: function () {
    return {
      hotcomments: [],
      comments: [],
      thisSong: {},
      lastrouter: null,
      total: 0,
      page: 1,
      complete: false,
      tocontinue: false,
      time: 0,
    };
  },
  components: {
    ComItem,
    Loading,
  },
  created: function () {
    this.time += new Date().getTime();

    this.complete = true;
    this.axios
      .get(
        `https://apis.netstart.cn/music/comment/music?id=${this.$route.query.id}&limit=20&offset=${this.page}`
      )
      .then((res) => {
        // console.log(res.data);
        this.page++;
        this.total = res.data.total;
        this.hotcomments = res.data.hotComments;
        this.comments = res.data.comments;
      })
      .then(
        this.axios
          .get(
            `https://apis.netstart.cn/music/song/detail?ids=${this.$route.query.id}`
          )
          .then((res) => {
            this.thisSong = res.data.songs[0];
            this.complete = false;
          })
      );
  },
  watch: {
    songid: function () {},
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      vm.lastrouter = from.fullPath;
    });
  },
  beforeRouteLeave(to, from, next) {
    // 导航离开该组件的对应路由时调用
    // 可以访问组件实例 `this`
    // console.log(to, from);
    next();
    this.$parent.showbar = true;
    this.$parent.$refs.play.popup = true;
  },
  methods: {
    toback() {
      this.$router.push({ path: this.lastrouter });
    },
    onscroll() {
      console.log(111);
      // console.log(this.$refs.comt.offsetTop);
    },
    load() {
      this.time = new Date().getTime();
      this.tocontinue = true;
      this.axios
        .get(
          `https://apis.netstart.cn/music/comment/music?id=${this.$route.query.id}&limit=20&offset=${this.page}`
        )
        .then((res) => {
          console.log(res.data);
          this.page++;
          this.comments = [...this.comments, ...res.data.comments];
          this.tocontinue = false;
        });
    },
  },
};
</script>

<style lang="less" scoped>
.comment {
  width: 100vw;
  height: 100vh;
  // padding: 10px 20px;
  background-color: #f5f5f5;
  position: relative;
  &.load {
    display: flex;
    align-items: center;
  }
  .back {
    // position: absolute;
    // top: 5px;
    // left: 5px;
    // padding: 5px 5px;
    padding: 10px 15px;

    background-color: #fff;
    // margin-bottom: 20px;
    z-index: 10;
    display: flex;
    align-items: center;
    img {
      width: 25px;
      height: 25px;
    }
    span {
      // width: 10vw;
      // position: absolute;
      margin-left: 10px;
      color: #000;
      // text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.2);
    }
  }
  .song {
    padding: 10px 15px;
    width: 100%;
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    .songimg {
      width: 12vw;
      height: 12vw;
      padding: 8px;
      background-image: url("../assets/c81.png");
      background-repeat: no-repeat;
      background-size: 100%;
      background-position: center;
      img {
        border-radius: 50%;
        vertical-align: middle;
      }
    }
    .songtitle {
      flex: 1;
      margin-left: 10px;
      // margin-bottom: 20px;
      display: flex;
      align-items: center;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      h1 {
        font-size: 16px;
        max-width: 30vw;
        color: #000;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        display: inline-block;
      }
      // float: left;
      // display: inline-block;
      color: #888888;
      font-size: 14px;
      width: 250px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      .name {
        &::after {
          content: "/";
        }
        &:last-of-type {
          &::after {
            content: " - ";
          }
        }
      }
    }
  }
  .com {
    margin-top: 10px;
    padding: 5px 0 5px 15px;
    background-color: #fff;
    h1 {
      font-size: 15px;
      // font-weight: bold;
      padding: 10px 0;
      position: sticky;
      top: 0;
      background-color: #fff;
      z-index: 2;
    }
  }
  h3 {
    text-align: center;
    padding: 10px 0;
    color: #ccc;
  }
}
</style>